package com.apestech.framework.run.datamapper;

/**
 * 字串方式变量组合
 *
 * @author hup
 * @create 2018-03-31-9:28
 */
public class StrSingleData {

    //ZCD.ZCCKID=CKID;SPID;MDID=BMID
    //MDID#DDID
    private String tableName=null;
    private String fieldName=null;
    private String target=null;
    private String text=null;
    private String express=null;
    private boolean isExpress;

    public StrSingleData(String text){
        this.text=text;
        init();
    }

    private void init(){
        if(this.text==null) return;
        this.text=this.text.trim();
        int pos=this.text.indexOf("=");
        if(pos>=0){
            this.fieldName=this.text.substring(0, pos);
            this.target=this.text.substring(pos+1, this.text.length());
        } else{
            this.fieldName=this.text;
        }
        pos=this.fieldName.indexOf(".");
        if(pos>=0){
            this.tableName=this.fieldName.substring(0, pos);
            this.fieldName=this.fieldName.substring(pos+1, this.fieldName.length());
        }
        if(this.target==null){
            this.target=this.fieldName;
        }

        this.isExpress=isExpress(this.target);
        if(this.isExpress){
            this.express=this.target;
            this.target=this.fieldName;
        }
    }

    private boolean isExpress(String text){
        if(text==null){
            return false;
        }
        String s=text.toUpperCase();
        if(text.indexOf("+")>0) return true;
        if(text.indexOf("-")>=0) return true;
        if(text.indexOf("*")>0) return true;
        if(text.indexOf("/")>0) return true;
        if(text.indexOf(" AND ")>0) return true;
        if(text.indexOf(" OR ")>0) return true;
        if(text.indexOf(" IF ")>=0) return true;
        if(text.indexOf(" WHILE ")>=0) return true;
        return false;
    }

    public String getTableName() {
        return tableName;
    }

    public String getFieldName() {
        return fieldName;
    }

    public String getTarget() {
        return target;
    }

    public boolean isExpress() {
        return isExpress;
    }

    public String getExpress() {
        return express;
    }
}
